#!/usr/bin/env python
# coding: utf-8
# @Time   : 2022/11/25 13:00
# @Author : 梁灿
import logging
import logging.handlers
import time
from common import get_project_path
import ctypes

STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE = -11
STD_ERROR_HANDLE = -12

FOREGROUND_BLACK = 0x00 # black.
FOREGROUND_DARKBLUE = 0x01 # dark blue.
FOREGROUND_DARKGREEN = 0x02 # dark green.
FOREGROUND_DARKSKYBLUE = 0x03 # dark skyblue.
FOREGROUND_DARKRED = 0x04 # dark red.
FOREGROUND_DARKPINK = 0x05 # dark pink.
FOREGROUND_DARKYELLOW = 0x06 # dark yellow.
FOREGROUND_DARKWHITE = 0x07 # dark white.
FOREGROUND_DARKGRAY = 0x08 # dark gray.
FOREGROUND_BLUE = 0x09 # blue.
FOREGROUND_GREEN = 0x0a # green.
FOREGROUND_SKYBLUE = 0x0b # skyblue.
FOREGROUND_RED = 0x0c # red.
FOREGROUND_PINK = 0x0d # pink.
FOREGROUND_YELLOW = 0x0e # yellow.
FOREGROUND_WHITE = 0x0f # white.

BACKGROUND_BLUE = 0x10 # dark blue.
BACKGROUND_GREEN = 0x20 # dark green.
BACKGROUND_DARKSKYBLUE = 0x30 # dark skyblue.
BACKGROUND_DARKRED = 0x40 # dark red.
BACKGROUND_DARKPINK = 0x50 # dark pink.
BACKGROUND_DARKYELLOW = 0x60 # dark yellow.
BACKGROUND_DARKWHITE = 0x70 # dark white.
BACKGROUND_DARKGRAY = 0x80 # dark gray.
# BACKGROUND_BLUE = 0x90 # blue.
# BACKGROUND_GREEN = 0xa0 # green.
BACKGROUND_SKYBLUE = 0xb0 # skyblue.
BACKGROUND_RED = 0xc0 # red.
BACKGROUND_PINK = 0xd0 # pink.
BACKGROUND_YELLOW = 0xe0 # yellow.
BACKGROUND_WHITE = 0xf0 # white.

std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)


def set_cmd_text_color(color, handle=std_out_handle):
    Bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
    return Bool


def reset():
    set_cmd_text_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)


def error(mess, end = '\n', flush = True):
    set_cmd_text_color(FOREGROUND_RED)
    print("[ERROR]", mess, end = end, flush = flush)
    reset()


def warn(mess, end = '\n', flush = True):
    set_cmd_text_color(FOREGROUND_YELLOW)
    print("[WARN]", mess, end = end, flush = flush)
    reset()


def info(mess, end = '\n', flush = True):
    set_cmd_text_color(FOREGROUND_GREEN)
    print("[INFO]", mess, end = end, flush = flush)
    reset()


def write(mess, foregound = FOREGROUND_WHITE, background = FOREGROUND_BLACK, end = '\n', flush = True):
    set_cmd_text_color(foregound | background)
    print(mess, end = end, flush = flush)
    reset()


def get_logger():
    # 创建logger
    logger = logging.getLogger()
    # 设置log日志级别
    logger.setLevel(logging.INFO)
    # 第二步，创建一个handler， 用于写入日志文件
    serf_time = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time()))
    project_path = get_project_path.get_project_path()  # 获取项目跟目录

    filename = project_path + '/log/' + serf_time + '.log'  # 定义日志存放路径及名称
    # 创建 hander,设置日志切割时间（半夜），按天存放，最多存放7个日志文件
    rf_handler = logging.handlers.TimedRotatingFileHandler(filename, when='midnight', interval=1, backupCount=7
                                                        , encoding="UTF-8")
    # 第三步，定义handler的输出格式
    data_format = "%Y-%m-%d %H:%M:%S"
    rf_handler.setFormatter(logging.Formatter("%(asctime)s-%(filename)s-%(funcName)s[line:%(lineno)d]-%(levelname)s:%("
                                          "message)s", datefmt=data_format))
    # 第四步，将logger添加到handler里面，并返回
    logger.addHandler(rf_handler)
    return logger


if __name__=='__main__':
    info("==============================")
    # def test01():
    #     logger = get_logger()
    #     logger1.debug('***debug***')
    #     logger.info('***信息***')
    #     logger.warning('***警告***')
    #     logger.error('***error***')
    #     logger.critical('***critical***')
    # test01()




